export default class Primitives {
  constructor(viewer) {
    this.viewer = viewer;
    this.primitives = viewer.scene.primitives;
  }

  //添加
  add(primitive) {
    return this.primitives.add(primitive);
  }

  //删除
  remove(primitive) {
    this.primitives.remove(primitive);
  }

  //创建容器
  createPrimitives() {
    return new Cesium.Primitive();
  }

  //primitive方式加载模型
  createPrimitiveModel({
    path,
    position,
    headingPitchRoll,
    fixedFrameTransform,
  }) {
    if (path == undefined || position == undefined) {
      return false;
    }
    let option = Cesium.Model.fromGltf({
      url: path,
      scale: 50,
      modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
        position, headingPitchRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform
      ),
      minimumPixelSize: 50,
      maximumScale: 1000
    });
    let primitive = this.add(option);
    primitive.readyPromise.then(model => {
      model.activeAnimations.addAll({
        speedup: 2,
        loop: Cesium.ModelAnimationLoop.REPEAT,
        reverse: true
      });
      var camera = this.viewer.camera;
      // Zoom to model
      var controller = this.viewer.scene.screenSpaceCameraController;
      var r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
      controller.minimumZoomDistance = r * 0.5;

      var center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, new Cesium.Cartesian3());
      var heading = Cesium.Math.toRadians(230.0);
      var pitch = Cesium.Math.toRadians(-20.0);
      camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 20.0));
    })
    return primitive;
  }

  //计算矩阵
  getModelMatrix(positions) {
    let lon = positions.lon;
    let lat = positions.lat;
    let alt = positions.alt;
    //地面位置(垂直地面)
    let bottom = Cesium.Cartesian3.fromDegrees(lon, lat);
    //中心位置
    let center = Cesium.Cartesian3.fromDegrees(lon, lat, alt / 2);
    //顶部位置
    let top = Cesium.Cartesian3.fromDegrees(lon, lat, alt);
    //矩阵计算
    let modelMatrix = Cesium.Matrix4.multiplyByTranslation(
      Cesium.Transforms.eastNorthUpToFixedFrame(bottom),
      new Cesium.Cartesian3(0.0, 0.0, alt * 0.5), new Cesium.Matrix4()
    );
    return modelMatrix;
  }

  //添加3DTileset类型
  create3DTileset(option) {
    let _3DTileset = new Cesium.Cesium3DTileset({
      url: option.url
    });
    let heightStyle = new Cesium.Cesium3DTileStyle({
      color: {
        conditions: [
          ['${height} >= 300', 'rgba(45, 0, 75, 0.3)'],
          ['${height} >= 200', 'rgba(102, 71, 151, 0.3)'],
          ['${height} >= 100', 'rgba(170, 162, 204, 0.3)'],
          ['${height} >= 50', 'rgba(224, 226, 238, 0.3)'],
          ['${height} >= 25', 'rgba(252, 230, 200, 0.3)'],
          ['${height} >= 10', 'rgba(248, 176, 87, 0.3)'],
          ['${height} >= 5', 'rgba(198, 106, 11, 0.3)'],
          ['true', 'rgba(127, 59, 8, 0.3)']
        ]
      }
    });
    _3DTileset.readyPromise.then((data) => {
      this.tilesetData = data;
      this.tilesetData.style = heightStyle;
      this.add(this.tilesetData);
      this.ccc.zoomTo(this.tilesetData);
    }).otherwise(function (error) {
      console.log(error);
    });
  }

  //删除3DTileset
  remove3DTileset() {
    if (this.tilesetData == undefined) {
      return false;
    }
    this.primitives.remove(this.tilesetData);
  }
}